# Maintainer: Caleb Maclennan <caleb@alerque.com>

pkgbase=lua-cosmo
_rockname=${pkgbase#lua-}
pkgname=("$pkgbase" "lua51-$_rockname" "lua52-$_rockname" "lua53-$_rockname")
pkgver=16.06.04
pkgrel=6
_rockrel=1
pkgdesc='Safe templates for Lua'
arch=(any)
url="https://github.com/mascarenhas/$_rockname"
license=(MIT)
_luadeps=(lpeg)
makedepends=(lua
             lua51
             lua52
             lua53
             luarocks)
checkdepends=("${_luadeps[@]/#/lua-}")
_archive="$_rockname-$pkgver"
_rockspec="$_archive-$_rockrel.rockspec"
source=("$url/archive/v$pkgver/$_archive.tar.gz")
sha256sums=('86d17aea5080a90671d965cffeb9b104c19e0e1ea55c08687c0924c4512b52b1')

prepare() {
	cd "$_archive"
	# Lpeg is not needed at *build* time, but `luarocks make` checks for it
	# and depends=() is set at package time too late for makepkg to install it
	sed -i -e '/lpeg >=/d' "rockspec/$_rockspec"
}

check() {
	cd "$_archive/src"
	lua ../tests/test_cosmo.lua
}

_package() {
	cd "$_archive"
	depends=("${pkgname%-*}" "${_luadeps[@]/#/${pkgname%-*}-}")
	luarocks --lua-version="$1" --tree="$pkgdir/usr/" \
		make --deps-mode=none --no-manifest "rockspec/$_rockspec"
	sed -ne '/license/p' rockspec/$_rockspec |
		install -Dm0644 /dev/stdin "$pkgdir/usr/share/licenses/$pkgname/LICENSE.lua"
}

package_lua-cosmo() {
	_package 5.4
}

package_lua51-cosmo() {
	_package 5.1
}

package_lua52-cosmo() {
	_package 5.2
}

package_lua53-cosmo() {
	_package 5.3
}
